<?php if (! defined('BASEPATH')) exit('No direct script access allowed');

class Category extends CI_Controller {

	public $data;

	public function __construct()
	{
		parent::__construct();
		$this->load->model('Category_model');
	}

	public function index()
	{
		//check if logged in
		if(null == $this->session->userdata('userInfo')) //if not login
			redirect('login');

		if(null != $this->session->userdata('loginError'))
			$this->session->unset_userdata('loginError');
		
		//get data from category table
		$categories = $this->Category_model->get_all();
		$userdata = $this->session->userdata('userInfo');

		$data = array(
			'title' => 'Khai nghiệp | Đối tác',
			'name' => $userdata['name'],
 			'categories' => $categories
		);

		$this->load->view('tmpl/header', $data);
		$this->load->view('tmpl/left', $data);
		$this->load->view('category_view', $data);
		$this->load->view('tmpl/footer');
	}

	public function get() {
		//check if logged in
		if(null == $this->session->userdata('userInfo')) //if not login
			redirect('login');

		if(null != $this->session->userdata('loginError'))
			$this->session->unset_userdata('loginError');

		$categories = $this->Category_model->get_categories();
		
		if($categories) {
			$data = array(
				'cates' => $categories
			);
			echo json_encode($data);
		}else
			return false;
	}


	public function add_new()
	{	
		//check if logged in
		if(null == $this->session->userdata('userInfo')) //if not login
			redirect('login');

		if(null != $this->session->userdata('loginError'))
			$this->session->unset_userdata('loginError');
	
		//get data
		$name = $_POST['name'];
		$father_id = $_POST['father_id'];

		if((null != $name) && (null != $father_id)) {//if data not null
			$new_data = array(
				'name' => $name,
				'father_id' => $father_id
			);
			$cond = array(
				'cate1.name' => $name,
				'cate1.father_id' => $father_id
			);
			$result = $this->Category_model->add_new($new_data, $cond); //id = -1: fail, else 
			//success

			if($result) { //insert success
				$rp = array('id' => $result[0]->id,
							'fname' => $result[0]->father_name
					);
				echo json_encode($rp);
			}else
				return false;
		}else
			return false;
	}

	//edit
	public function edit()
	{	
		//check if logged in
		if(null == $this->session->userdata('userInfo')) //if not login
			redirect('login');

		if(null != $this->session->userdata('loginError'))
			$this->session->unset_userdata('loginError');
		
		//get data
		$id = $_POST['id'];
		$name = $_POST['name'];
		$father_id = $_POST['father_id'];

		if((null != $id) && (null != $name) && (null != $father_id)) {//if data not null
			$cond = array(
				'id' => $id
			);

			$new_data = array(
				'name' => $name,
				'father_id' => $father_id
			);
 
			//success
			if($this->Category_model->edit($new_data, $cond)) { //update success
				return true;
			}else
				return false;
		}else
			return false;
	}

	//edit
	public function edit_father_id()
	{	
		//check if logged in
		if(null == $this->session->userdata('userInfo')) //if not login
			redirect('login');

		if(null != $this->session->userdata('loginError'))
			$this->session->unset_userdata('loginError');
		
		//get data
		$id = $_POST['id'];
		$father_id = $_POST['father_id'];

		if((null != $id) && (null != $father_id)) {//if data not null
			$cond = array(
				'id' => $id
			);

			$new_data = array(
				'father_id' => $father_id
			);
 
			//success
			if($this->Category_model->edit($new_data, $cond)) { //update success
				return true;
			}else
				return false;
		}else
			return false;
	}

	//delete
	public function delete()
	{
		//check if logged in
		if(null == $this->session->userdata('userInfo')) //if not login
			redirect('login');

		if(null != $this->session->userdata('loginError'))
			$this->session->unset_userdata('loginError');
		
		//get data
		$id = $_POST['id'];
		if(null != $id) {
			$cond = array(
				'id' => $id
			);
			
			if($this->Category_model->delete($cond)) { //delete success
				return true;
			}else
				return false;

		}else 
			return false;
	}
}

/* End of file welcome.php */
/* Location: ./application/controllers/welcome.php */